package com.mezz.bones.framework.core.helper;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author: zhuang
 * @Date: 2024/12/9 11:41
 * @Description: TODO
 * @Version: 1.0
 **/

@Component
public class SnowflakeIdGeneratorHelper {

    private final long workerId;
    private final long datacenterId;

    public SnowflakeIdGeneratorHelper(@Value("${snowflake.worker-id:0}") long workerId,
                                      @Value("${snowflake.datacenter-id:0}") long datacenterId) {
        // 设置默认值0
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    public long nextId() {
        //内部 nextId有做加锁处理
        return IdUtil.getSnowflake(workerId, datacenterId).nextId();
    }
}